<?php

class Application_Bootstrap  extends Engine_Bootstrap{

	//Глобальный инициализатор
	public static function init() {
		session_start();
        self::initConfig('application.ini');
        self::initHttpRequest();
		self::initDbAdapter(null);
        self::initActiveUser();
		return new Application_Bootstrap;
	}

	// Получить конфиг приложения в виде массива
	public static function getConfig($config = 'application.ini')
	{
		if (self::$__config === null) {
			self::initConfig($config);
		}
		return self::$__config;
	}

    /**
     * @return DbSimple_Mysql
     */
	public static function getDbAdapter($adapter = null)
	{
		$adapter = $adapter === null ? self::$__config['defaults']['dbAdapter'] : $adapter;
		if (self::$__dbAdapter[$adapter] === null) {
			self::initDbAdapter($adapter);
		}
		return self::$__dbAdapter[$adapter];
	}

    /**
     * @return Engine_Http_Request
     */
    public static function getRequest() {
		if (self::$__request === null) {
			self::initHttpRequest();
		}
		return self::$__request;
	}

    /**
     * @return Engine_User_Active
     */
    public static function getActiveUser() {
        if (self::$__activeUser === null) {
            self::initActiveUser();
        }
        return self::$__activeUser;
    }

    public static function run() {
    	
        $config = self::$__config;
        $http = new Engine_Http_Request();
        $class = 'Module_' . ucfirst($http->module) . '_Controller_' . ucfirst($http->controller);
        if (class_exists($class)) {
            $controller = new $class();
            $controller->preDispatch();
            $action = ($http->action) . 'Action';
            if (method_exists($class, $action)) {
            $controller->{$action}();
            $controller->run();
            } else {
                echo "NotFound";
            }
        } else {
           echo "NotFound";
        }

    }

}